package com.itheima.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.itheima.canal.config.RabbitMQConfig;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ZJ
 */
@CanalEventListener
public class SpuListener {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @ListenPoint(schema = "changgou_goods", table = "tb_spu")
    public void goodUp(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        Map<String, String> oldMap = new HashMap<>();
        rowData.getBeforeColumnsList().forEach((c) -> oldMap.put(c.getName(), c.getValue()));

        Map<String, String> newMap = new HashMap<>();
        rowData.getAfterColumnsList().forEach((c) -> newMap.put(c.getName(), c.getValue()));
/*
        if ("1".equals(oldMap.get("is_marketable")) && "0".equals(newMap.get("is_marketable"))) {
            rabbitTemplate.convertAndSend(RabbitMQConfig.GOODS_DOWN_EXCHANGE, "", newMap.get("id"));
        }*/

        //获取最新审核商品
        if ("0".equals(oldMap.get("status")) && "1".equals(newMap.get("status"))) {
            rabbitTemplate.convertAndSend(RabbitMQConfig.GOODS_UP_EXCHANGE, "", newMap.get("id"));
        }
    }
}
